var http = require("http");
var url = require("url");
var fs= require("fs");

var static_path = '.';

function start(route,handle) {
  function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    var realPath = static_path + pathname;
    console.log("About to route a request for " + pathname);
    fs.exists(realPath, function (exists) {
      if(!exists) {
          route(handle,pathname,request,response);
      } else {
          fs.readFile(realPath, 'binary', function(err, file) {
              if (err) {
                  response.writeHead(500, {'Content-Type': 'text/plain'});
                  response.write("This request URL " + pathname + " was not file.");
                  response.end();
              }else{
                  response.writeHead(200, {'Content-Type': 'text/html'});
                  response.write(file, "binary");
                  response.end();
              }
          });
      }
    });
  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}


exports.start = start;